<template>
  <div>
    <el-divider content-position="center">设计参数</el-divider>
    <div style="
        display: flex;
        justify-content: flex-end;
        margin-bottom: 20px;
        margin-right: 20px;
      ">
      <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddatmosphere">添加</el-button>
    </div>
    <el-table :data="ttaskConditionParamList" :row-class-name="rowDetailIndex" ref="tProductFailureDetail"
      max-height="300" border>
      <el-table-column label="序号" align="center" prop="index" width="50" />
      <el-table-column label="工况点编号" align="center" prop="conditionCode" min-width="150" show-overflow-tooltip />
      <el-table-column label="气体常数 R (kJ/kg·K)" align="center" prop="gasConstantR" min-width="200"
        show-overflow-tooltip>
        <template #default="{ row }">
          <!-- 空值显示 -，否则保留两位 -->
          <span>{{
            row.gasConstantR == null
              ? '-'
              : Number(row.gasConstantR).toFixed(2)
            }}</span>
        </template>
      </el-table-column>
      <el-table-column label="含湿量 X (kg/kg)" align="center" prop="humidityRatioX" min-width="150"
        show-overflow-tooltip >
          <template #default="{ row }">
          <!-- 空值显示 -，否则保留两位 -->
          <span>{{
            row.humidityRatioX == null
              ? '-'
              : Number(row.humidityRatioX).toExponential(3)
            }}</span>
        </template>
        </el-table-column>
      <el-table-column label="压比" align="center" prop="compressionRatio" min-width="150" show-overflow-tooltip />
      <el-table-column label="入口态体积流量 (m³/min)" align="center" prop="inletFlowActualM3Min" min-width="180"
        show-overflow-tooltip />
      <el-table-column label="标态流量(干) (Nm³/min)" align="center" prop="standardFlowDryNm3Min" min-width="180"
        show-overflow-tooltip />
      <el-table-column label="标态流量(湿) (Nm³/min)" align="center" prop="standardFlowWetNm3Min" min-width="180"
        show-overflow-tooltip />
      <el-table-column label="标态流量(湿) (Nm³/h)" align="center" prop="standardFlowWetNm3H" min-width="180"
        show-overflow-tooltip />
      <el-table-column label="质量流量(干) (kg/s)" align="center" prop="massFlowDryKgS" min-width="150"
        show-overflow-tooltip />
      <el-table-column label="质量流量(湿) (kg/s)" align="center" prop="massFlowWetKgS" min-width="150"
        show-overflow-tooltip />
      <el-table-column label="质量流量(干) (T/h)" align="center" prop="massFlowDryTH" min-width="150"
        show-overflow-tooltip />
      <el-table-column label="进气法兰处压力 pj (bar a)" align="center" prop="inletPressurePjBarA" min-width="150"
        show-overflow-tooltip />
      <el-table-column label="进气温度 Tj (℃)" align="center" prop="inletTempTjC" min-width="150" show-overflow-tooltip />
      <el-table-column label="进气温度 Tj (K)" align="center" prop="inletTempTjK" min-width="150" show-overflow-tooltip />
      <el-table-column label="相对湿度 (%)" align="center" prop="relativeHumidity" min-width="150" show-overflow-tooltip />
      <el-table-column label="排气法兰处压力 (bar a)" align="center" prop="outletPressureBarA" min-width="180"
        show-overflow-tooltip />
      <el-table-column label="排气法兰处压力 (kgf/cm² G)" align="center" prop="outletPressureKgfCm2G" min-width="200"
        show-overflow-tooltip />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="120">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleUpdateatmosphere(scope.row)">修改</el-button>
          <el-button size="mini" type="text" @click="handleDeleteatmosphere(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="设计参数" :close-on-click-modal="false" :visible.sync="Open" width="900px" append-to-body
      v-dialogDrag>
      <el-form ref="forms" :model="forms" label-width="200px" :inline="true" class="addData">
        <el-form-item label="工况点编号" prop="conditionCode">
          <el-input v-model="forms.conditionCode" placeholder="请输入工况点编号" />
        </el-form-item>
        <el-form-item label="气体常数 R (kJ/kg·K)" prop="gasConstantR">
          <el-input v-model="forms.gasConstantR" placeholder="请输入气体常数 R (kJ/kg·K)" />
        </el-form-item>
        <el-form-item label="含湿量 X (kg/kg)" prop="humidityRatioX">
          <el-input v-model="forms.humidityRatioX" placeholder="请输入含湿量 X (kg/kg)" />
        </el-form-item>
        <el-form-item label="压比" prop="compressionRatio">
          <el-input v-model="forms.compressionRatio" placeholder="请输入压比" />
        </el-form-item>
        <el-form-item label="入口态体积流量 (m³/min)" prop="inletFlowActualM3Min">
          <el-input v-model="forms.inletFlowActualM3Min" placeholder="请输入入口态体积流量 (m³/min)" />
        </el-form-item>
        <el-form-item label="标态流量(干) (Nm³/min)" prop="standardFlowDryNm3Min">
          <el-input v-model="forms.standardFlowDryNm3Min" placeholder="请输入标态流量(干) (Nm³/min)" />
        </el-form-item>
        <el-form-item label="标态流量(湿) (Nm³/min)" prop="standardFlowWetNm3Min">
          <el-input v-model="forms.standardFlowWetNm3Min" placeholder="请输入标态流量(湿) (Nm³/min)" />
        </el-form-item>
        <el-form-item label="标态流量(湿) (Nm³/h)" prop="standardFlowWetNm3H">
          <el-input v-model="forms.standardFlowWetNm3H" placeholder="请输入标态流量(湿) (Nm³/h)" />
        </el-form-item>
        <el-form-item label="质量流量(干) (kg/s)" prop="massFlowDryKgS">
          <el-input v-model="forms.massFlowDryKgS" placeholder="请输入质量流量(干) (kg/s)" />
        </el-form-item>
        <el-form-item label="质量流量(湿) (kg/s)" prop="massFlowWetKgS">
          <el-input v-model="forms.massFlowWetKgS" placeholder="请输入质量流量(湿) (kg/s)" />
        </el-form-item>
        <el-form-item label="质量流量(干) (T/h)" prop="massFlowDryTH">
          <el-input v-model="forms.massFlowDryTH" placeholder="请输入质量流量(干) (T/h)" />
        </el-form-item>
        <el-form-item label="进气法兰处压力 pj (bar a)" prop="inletPressurePjBarA">
          <el-input v-model="forms.inletPressurePjBarA" placeholder="请输入进气法兰处压力 pj (bar a)" />
        </el-form-item>
        <el-form-item label="进气温度 Tj (℃)" prop="inletTempTjC">
          <!-- <el-input
            v-model="forms.inletTempTjC"
            placeholder="请输入进气温度 Tj (℃)"
          /> -->
          <el-input-number placeholder="请输入进气温度 Tj (℃)" :controls="false" v-model="forms.inletTempTjC"
            @change="handleChange" :min="1" :max="10000000000" label="请输入进气温度 Tj (℃)"></el-input-number>
        </el-form-item>
        <el-form-item label="进气温度 Tj (K)" prop="inletTempTjK">
          <el-input disabled v-model="forms.inletTempTjK" placeholder="请输入进气温度 Tj (K)" />
        </el-form-item>
        <el-form-item label="相对湿度 (%)" prop="relativeHumidity">
          <el-input v-model="forms.relativeHumidity" placeholder="请输入相对湿度 (%)" />
        </el-form-item>
        <el-form-item label="排气法兰处压力 (bar a)" prop="outletPressureBarA">
          <el-input v-model="forms.outletPressureBarA" placeholder="请输入排气法兰处压力 (bar a)" />
        </el-form-item>
        <el-form-item label="排气法兰处压力 (kgf/cm² G)" prop="outletPressureKgfCm2G">
          <el-input v-model="forms.outletPressureKgfCm2G" placeholder="请输入排气法兰处压力 (kgf/cm² G)" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormatmosphere">确 定</el-button>
        <el-button @click="cancelatmosphere">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      currentEditIndex: -1,
      ttaskConditionParamList: [],
      Open: false,
      forms: {},
    };
  },
  props: {
    formData: {
      type: Array,
      default: () => [],
    },
  },
  watch: {
    formData: {
      immediate: true,
      deep: true,
      handler(newVal) {
        // 更新组件内部数据
        this.ttaskConditionParamList = JSON.parse(JSON.stringify(newVal));
      },
    },
  },

  methods: {
    handleChange(e) {
      this.forms.inletTempTjK = (Number(e) + 273.15).toFixed(2)
    },
    rowDetailIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    handleAddatmosphere() {
      this.Open = true;
      this.currentEditIndex = -1;
      this.resetform();
    },
    // 修改
    handleUpdateatmosphere(row) {
      this.Open = true;
      this.forms = { ...row };

      // 查找当前编辑项的索引
      if (row.id) {
        this.currentEditIndex = this.ttaskConditionParamList.findIndex(
          (item) => item.id === row.id
        );
      } else {
        this.currentEditIndex = this.ttaskConditionParamList.findIndex((item) =>
          this.isSameRow(item, row)
        );
      }
    },

    // 删除
    handleDeleteatmosphere(row) {
      this.$confirm("确认删除设计参数数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          if (row.id) {
            // 根据ID删除
            const index = this.ttaskConditionParamList.findIndex(
              (item) => item.id === row.id
            );
            if (index !== -1) {
              this.ttaskConditionParamList.splice(index, 1);
            }
          } else {
            // 没有ID，根据索引删除
            const index = this.ttaskConditionParamList.findIndex((item) =>
              this.isSameRow(item, row)
            );
            if (index !== -1) {
              this.ttaskConditionParamList.splice(index, 1);
            }
          }
          this.$message.success("删除成功!");
        })
        .catch(() => {
          this.$message.info("已取消删除");
        });
    },
    resetform() {
      this.forms = {
        angle: undefined,
        dischargePressure: undefined,
      };
    },
    // 生成临时ID
    generateTempId() {
      return "temp_" + Date.now() + Math.floor(Math.random() * 1000);
    },
    // 判断两行数据是否相同
    isSameRow(row1, row2) {
      const keys = Object.keys(row1);
      return keys.every((key) => row1[key] === row2[key]);
    },
    submitFormatmosphere() {
      this.$refs.forms.validate((valid) => {
        if (valid) {
          const formData = { ...this.forms };
          if (!this.ttaskConditionParamList) {
            this.ttaskConditionParamList = [];
          }
          if (this.currentEditIndex === -1) {
            // 新增操作
            if (!formData.id) {
              formData.temp_id = this.generateTempId(); // 为新增数据生成临时ID
            }

            this.ttaskConditionParamList.push(formData);
          } else {
            // 修改操作
            if (formData.id) {
              // 根据ID修改
              const index = this.ttaskConditionParamList.findIndex(
                (item) => item.id === formData.id
              );
              if (index !== -1) {
                this.ttaskConditionParamList.splice(index, 1, formData);
              }
            } else {
              // 没有ID，根据行数据匹配修改
              this.ttaskConditionParamList.splice(
                this.currentEditIndex,
                1,
                formData
              );
            }
          }
          this.$emit(
            "getTtaskConditionParamList",
            this.ttaskConditionParamList
          );
          this.Open = false;
          this.resetform();
        }
      });
    },
    cancelatmosphere() {
      this.Open = false;
      this.currentEditIndex = -1;
      this.resetform();
    },
  },
};
</script>
<style scoped lang="scss"></style>
